using UnityEngine;
using UnityEditor; 
using System.Collections;
using System.Collections.Generic;


[CustomEditor(typeof(Character))]  
public class CharacterInspector : Editor
{
	private string folder = System.IO.Directory.GetCurrentDirectory();
	private string xmlPath = "None";
	private Character character;
  

	public override void OnInspectorGUI () 
	{  

		character = (Character)target;

		//drawSaveLoad();
 
		DrawDefaultInspector();
		character.IndexStates();


		//drawStateMap();
		//drawAddNewState();
	}  

	void drawSaveLoad()
	{
		EditorGUILayout.BeginVertical();
		EditorGUILayout.Separator();
		EditorGUILayout.LabelField("Data Path:");
		xmlPath = GUILayout.TextArea(xmlPath); 
		EditorGUILayout.BeginHorizontal();
		bool load = GUILayout.Button("Load");
		bool save = GUILayout.Button("Save");
		bool saveAs = GUILayout.Button("Save As");
		EditorGUILayout.EndHorizontal();

		EditorGUILayout.EndVertical();
		EditorGUILayout.Separator();

		if(load)
		{
			string newPath = UnityEditor.EditorUtility.OpenFilePanel(
				"Select data file", folder, "xml");

			if(newPath != "")
			{
				xmlPath = newPath; 
				folder = newPath.Remove(newPath.LastIndexOf("/"));

				//character = XMLSaveLoad.DeserializeObject<CharacterData>(newPath);
			}

		}

		if(save)
		{
			//XMLSaveLoad.SerializeObject<CharacterData>(character.data, xmlPath);
		}

		if(saveAs)
		{
			string newPath = UnityEditor.EditorUtility.SaveFilePanel(
				"Select data file", folder, character.characterName, "xml");
			
			if(newPath != "")
			{
				xmlPath = newPath;
				folder = newPath.Remove(newPath.LastIndexOf("/"));
			}

			//XMLSaveLoad.SerializeObject<CharacterData>(character.data, xmlPath);
		}
	}

	void drawStateMap()
	{
		EditorGUILayout.LabelField("State Map");

		foreach (KeyValuePair<string, CharacterAnimatedState> kv in character.stateIndex)
		{
			if(kv.Value == null)
			{
				Debug.LogError("MLGB");
			}
		}


		return;


		string[] keys = new string[character.stateIndex.Count];
		character.stateIndex.Keys.CopyTo(keys, 0);

		for(int i = 0; i < keys.Length; i++)
		{
			// draw
			EditorGUILayout.BeginHorizontal();
			bool remove = GUILayout.Button("-", GUILayout.MaxWidth(16));
			string newKey = EditorGUILayout.TextField(keys[i]);
			EditorGUILayout.EndHorizontal();
 
			// change key
			if(newKey != keys[i])
			{
				CharacterAnimatedState value = character.stateIndex[keys[i]];
				character.stateIndex.Add(newKey, value); 
				character.stateIndex.Remove(keys[i]);
			}

			// remove
			if(remove)
			{
				character.stateIndex.Remove(keys[i]);
			}
		}
	}

	void drawAddNewState()
	{
		// draw
		EditorGUILayout.BeginHorizontal();
		string newState = EditorGUILayout.TextField("New State", GUILayout.MaxWidth(120));
		bool add = GUILayout.Button("Add", GUILayout.MaxWidth(50)); 
		EditorGUILayout.EndHorizontal();

		// add state
		if(add)
		{
			int index = 1;
			if(character.stateIndex.ContainsKey(newState))
			{
				while(character.stateIndex.ContainsKey(newState + index))
				{
					index++;
				}
				newState = newState + index;
			}
 

			character.stateIndex[newState] = new CharacterAnimatedState();

		}
	}  
}

